package com.senriot.quickup.service.mapper

import com.senriot.cloud.common.IEntityMapper
import com.senriot.quickup.domain.User
import com.senriot.quickup.service.dto.StaffDTO
import com.senriot.quickup.service.dto.UserDTO
import org.mapstruct.Mapper
import org.mapstruct.Mapping
import org.mapstruct.Mappings
import org.mapstruct.NullValuePropertyMappingStrategy

/**
 * Mapper for the entity [User] and its DTO called [UserDTO].
 *
 * Normal mappers are generated using MapStruct, this one is hand-coded as MapStruct
 * support is still in beta, and requires a manual step with an IDE.
 */
@Mapper(
        componentModel = "spring",
        uses = [RoleMapper::class, CityMapper::class, AgencyMapper::class],
        nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE
)
abstract class UserMapper : IEntityMapper<UserDTO, User>
{
    @Mappings(
            Mapping(target = "cityId", source = "city.id"),
            Mapping(target = "provinceId", source = "province.id"),
            Mapping(target = "districtId", source = "district.id"),
            Mapping(target = "cityName", source = "city.name"),
            Mapping(target = "provinceName", source = "province.name"),
            Mapping(target = "districtName", source = "district.name")
    )
    abstract override fun toDto(entity: User): UserDTO

    @Mappings(
            Mapping(target = "city", source = "cityId"),
            Mapping(target = "province", source = "provinceId"),
            Mapping(target = "district", source = "districtId"),
            Mapping(target = "agency", source = "agencyId")
    )
    abstract override fun toEntity(dto: UserDTO): User


    @Mappings(
            Mapping(target = "cityId", source = "city.id"),
            Mapping(target = "provinceId", source = "province.id"),
            Mapping(target = "districtId", source = "district.id"),
            Mapping(target = "cityName", source = "city.name"),
            Mapping(target = "provinceName", source = "province.name"),
            Mapping(target = "districtName", source = "district.name"),
            Mapping(target = "agencyId", source = "agency.id"),
            Mapping(target = "agencyName", source = "agency.name")
    )
    abstract fun toStaffDTO(user: User): StaffDTO


    fun fromId(id: Long?) = id?.let { User(it) }
}
